﻿using System.Collections.Generic;
using System.IO;
using System.Reflection;
using LLoger;

namespace LimitatorInterfaces
{
    public abstract class ServiceBase : IService
    {
        protected readonly ILogWriter LogWriter;
        private readonly ConfigReader _configReader;
        private Dictionary<string, object> _serviceParams;

        protected ServiceBase(string configPath, ILogWriter logWriter, Dictionary<string, object> serviceParams = null)
        {
            LogWriter = logWriter ?? new DummyLog();

            if (!File.Exists(configPath))
                configPath = Path.Combine(configPath, Assembly.GetAssembly(this.GetType()).GetName().Name + ".dll");

            // configPath = Path.Combine(configPath);

            _configReader = new ConfigReader(configPath, LogWriter);

            _serviceParams = serviceParams != null ? new Dictionary<string, object>(serviceParams) : new Dictionary<string, object>();
        }

        public object GetServiceParam(string key)
        {
            if (_serviceParams == null)
                return null;

            object param = "";
            _serviceParams.TryGetValue(key, out param);
            return param;
        }
        public KeyValuePair<string, string>[] GetStartWithSettingValues(string key)
        {
            return _configReader.GetStartWithSettingValues(key);
        }

        public void SetServiceParam(string key, object value)
        {
            if (_serviceParams.ContainsKey(key))
                _serviceParams[key] = value;
            else
                _serviceParams.Add(key, value);

        }

        public string GetSettingsValue(string key)
        {
            return _configReader.GetSettingValue(key);
        }
    }
}